﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using Caliburn.Micro;
using JY.Wpf.Controls.ControlHelpers;
using JY.Wpf.Core.Tools;

namespace JY.Wpf.Controls.Controls
{
    /// <summary>
    /// 遮罩
    /// </summary>
    public class BusyIndicator : ContentControl
    {
        #region # 变量
        public static readonly DependencyProperty BusyContentProperty = DependencyProperty.Register("BusyContent", typeof(object), typeof(BusyIndicator), new PropertyMetadata((PropertyChangedCallback)null));
        public static readonly DependencyProperty IsBusyProperty = DependencyProperty.Register("IsBusy", typeof(bool), typeof(BusyIndicator), new PropertyMetadata(false, new PropertyChangedCallback(BusyIndicator.OnIsBusyChanged)));

        /// <summary>
        /// 是否显示遮罩
        /// </summary>
        public bool IsBusy
        {
            get { return (bool)this.GetValue(BusyIndicator.IsBusyProperty); }
            set { this.SetValue(BusyIndicator.IsBusyProperty, value); }
        }
        /// <summary>
        /// 遮罩内容
        /// </summary>
        public object BusyContent
        {
            get { return this.GetValue(BusyIndicator.BusyContentProperty); }
            set { this.SetValue(BusyIndicator.BusyContentProperty, value); }
        } 
        #endregion

        /// <summary>
        /// 构造器
        /// </summary>
        public BusyIndicator()
        {
            this.Visibility = Visibility.Collapsed;
        }

        #region # 是否显示遮罩变更事件
        /// <summary>
        /// 是否显示遮罩变更事件
        /// </summary>
        private static void OnIsBusyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((BusyIndicator)d).Visibility = (bool)e.NewValue ? Visibility.Visible : Visibility.Collapsed;
        }
        #endregion

        #region # 激活遮罩层——void BusyOperation(bool isBusy,object busyContent=null)
        /// <summary>
        /// 激活遮罩层
        /// </summary>
        /// <returns></returns>
        public static void BusyOperation(bool isBusy,object busyContent=null)
        {
            if (isBusy)
            {
                #region # 显示遮罩层
                var window = VisualHelper.GetActiveWindow() ?? Application.Current.MainWindow;
                if (window == null) return;
                var decorator = VisualHelper.GetChild<AdornerDecorator>(window);
                if (decorator == null) return;
                var busyIndicator = new BusyIndicator();

                var layer = decorator.AdornerLayer;
                if (layer == null) return;
                var container = new AdornerContainer(layer)
                {
                    Child = busyIndicator,
                };
                busyIndicator.IsBusy = true;
                busyIndicator.BusyContent = busyContent;
                layer.Add(container);
                #endregion
            }
            else
            {
                #region # 关闭遮罩层

                var windows = Application.Current.Windows.OfType<Window>().ToList();
                foreach (Window currentWindow in windows)
                {
                    if (currentWindow == null) continue;
                    var decorator = VisualHelper.GetChild<AdornerDecorator>(currentWindow);
                    if (decorator == null) continue;
                    if (decorator.Child != null)
                    {
                        decorator.Child.IsEnabled = true;
                    }
                    var layer = decorator.AdornerLayer;
                    var adorners = layer.GetAdorners(decorator.AdornerLayer);
                    if (adorners == null) continue;
                    foreach (var adorner in adorners.Where(x => x is AdornerContainer).Select(x => x as AdornerContainer))
                    {
                        if (adorner.Child is BusyIndicator)
                        {
                            layer?.Remove(adorner);
                        }
                    }
                }
                #endregion
            }
        }
        #endregion
    }
}
